package cn.itcast.service.impl;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.mapper.MemberMapper;
import cn.itcast.mapper.OrderMapper;
import cn.itcast.mapper.OrderSettingListMapper;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.User;
import cn.itcast.service.OrderSettingListService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {
    @Autowired
    OrderSettingListMapper orderSettingListMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public PageResult findPage(QueryPageBean bean) {
        PageHelper.startPage(bean.getCurrentPage(),bean.getPageSize());

        List<Map<String,Object>> map=orderSettingListMapper.findPage(bean.getQueryString());
        for (Map<String, Object> map1 : map) {
            Date orderDate = (Date) map1.get("orderDate");
            map1.put("orderDate",new SimpleDateFormat("yyyy年MM月dd日").format(orderDate));
        }
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(map);
        return new PageResult(mapPageInfo.getTotal(),map );
    }

    @Override
    public void add(Map map,Integer id) throws  Exception {
        //首先获得电话号码
        String phoneNumber = (String) map.get("phoneNumber");
        //通过电话号码查询会员.
        Member member = memberMapper.findByTelephone(phoneNumber);
        //如果没有会员就帮他创建一个会员;
        if (member==null){
            member=new Member();
            map.put("regTime",new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.getToday() ));
            Date date=new Date();
            String fileNumber = new SimpleDateFormat("yyyyMMddmmss").format(date);
            map.put("fileNumber", fileNumber);
            map.put("phoneNumber", map.get("phoneNumber").toString());
            map.put("name", map.get("name").toString());
            map.put("sex", map.get("sex").toString());
            map.put("idCard", map.get("IdCard").toString());
            memberMapper.addOrder(map);
            Integer m_id =memberMapper.getMaxId();
            member.setId(m_id);
        }
        //创建会员后给他添加她相对应的体检套餐;
        Integer orderType = Integer.parseInt((String) map.get("orderType"));
        Integer orderStatus = Integer.parseInt((String) map.get("orderStatus"));
        String type= orderType == 1 ? "微信预约" : "电话预约";
        String status = orderStatus == 1 ? "已确认" : "未确认";
        Order order=new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date((String) map.get("orderDate")));
        order.setOrderType(type);
        order.setOrderStatus(status);
        order.setSetmealId(id);
        orderMapper.add(order);

    }

    @Override
    public void delete(Integer id) {
        orderMapper.delete(id);
    }

    @Override
    public Result updata(Integer id) {
        Map<String,Object> map=orderMapper.findById(id);
        String  orderStatus = (String) map.get("orderStatus");
        if (orderStatus.equals("已确认")){
            return new Result(false,"已经是确认,无法再次确认" );
        }else {
            orderMapper.updataStatus(id);
            return new Result(true,"已提交预约" );
        }
    }
}
